using System;
using System.Collections.Generic;
using Utilities.Miscellaneous;

namespace Utilities.Collections
{
  public class PropertyComparer<TObject, TProperty> : IComparer<TObject> where TProperty : IComparable<TProperty>
  {
    private readonly Func<TObject, TProperty> _propertyGetter;

    public PropertyComparer(Func<TObject, TProperty> propertyGetter)
    {
      ValidationUtils.ArgumentNotNull(propertyGetter, "propertyGetter");

      _propertyGetter = propertyGetter;
    }

    public int Compare(TObject x, TObject y)
    {
      TProperty xProperty = _propertyGetter(x);
      TProperty yProperty = _propertyGetter(y);

      return Comparer<TProperty>.Default.Compare(xProperty, yProperty);
    }
  }
}